package com.wang.cloud.common.encrypt;

import com.wang.cloud.common.encrypt.config.EncryptProperties;
import com.wang.cloud.common.encrypt.service.ApiEncryptService;
import com.wang.cloud.common.encrypt.service.ApiEncryptServiceAesImpl;
import com.wang.cloud.common.encrypt.service.ApiEncryptServiceSmImpl;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;


/**
 * @author wangye
 * @date 2023/7/11 0011
 */
@Slf4j
@EnableConfigurationProperties
@ComponentScan(basePackages = {"com.wang.cloud.common.encrypt"})
public class EnableAutoConfiguration {

    @PostConstruct
    public void init() {
        log.info("加载common-encrypt模块");
    }


    @ConfigurationProperties("common.encrypt")
    @Bean
    public EncryptProperties commonEncryptProperties() {
        return new EncryptProperties();
    }

    @Bean
    public ApiEncryptService apiEncryptService(EncryptProperties encryptProperties) {
        String type = encryptProperties.getType();
        String key = encryptProperties.getKey();
        if ("SM4".equals(type)) {
            return new ApiEncryptServiceSmImpl(key);
        } else if ("AES".equals(type)) {
            return new ApiEncryptServiceAesImpl(key);
        } else {
            throw new IllegalArgumentException("加密类型错误");
        }
    }
}
